﻿using DMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace DMS.Utils
{
    public class AccountRoleProvider : RoleProvider
    {
        private readonly Security _security = new Security();

        public override bool IsUserInRole(string username, string roleName)
        {
            //using (var usersContext = new UsersContext())
            //{
            //    var user = usersContext.Users.SingleOrDefault(u => u.UserName == username);
            //    if (user == null)
            //        return false;
            //    return user.UserRoles != null && user.UserRoles.Select(u => u.Role).Any(r => r.RoleName == roleName);
            //}
            AccountObject accountInfo = _security.CurrentUser(HttpContext.Current);
            if (accountInfo == null)
            {
                return false;
            }
            return accountInfo != null && accountInfo.Role_Name == roleName;
            //return accountInfo != null && accountInfo.Account_Role == Convert.ToInt32(roleName);
        }

        /// <summary>
        /// Ham su ly khi goi ham: FormsAuthentication.SetAuthCookie(account, true or false);
        /// </summary>
        /// <param name="username">account</param>
        /// <returns>tra ve quyen cua tai khoan</returns>
        public override string[] GetRolesForUser(string username)
        {
            //LoginAccountEntities db = new LoginAccountEntities(); //tao ket noi voi database
            //Account account = db.Accounts.Single(x => x.NameAccount.Equals(username)); //tuong duong voi cau lenh "Select Top 1 From Accounts Where NameAccount = username"
            //if (account != null) //neu tim thay tai khoan
            //{
            //    return new String[] { account.Roles }; //tra ve quyen cua nguoi dung
            //}
            //else
            //    return new String[] { }; //new khong tim thay tai khoan thi gan quyen bang null

            AccountObject accountInfo = new AccountObject();
            accountInfo = _security.CurrentUser(HttpContext.Current);
            if (accountInfo != null)
            {
                return new String[] { accountInfo.Account_Role.ToString() };
            }
            return new String[] { };
        }
        //public override bool IsUserInRole(string username, string roleName)
        //{
        //    throw new NotImplementedException();
        //}
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }



        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}